import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import Home from '../views/home.vue'
import About from '../views/about.vue'
import AdminEbook from '../views/admin/admin-ebook.vue'
import Doc from '../views/doc.vue'
import AdminUser from '../views/admin/admin-user.vue'
import AdminCategory from '../views/admin/admin-category.vue'
import AdminDoc from '../views/admin/admin-doc.vue'

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        name: 'home',
        component: Home,
        meta: {
            key: '/',
        }
    }, {
        path: '/doc',
        name: 'Doc',
        component: Doc,
        meta: {
            key: '/doc',
        }
    },
    {
        path: '/about',
        name: 'About',
        component: About,
        meta: {
            key: '/about',
        }
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        // component: () => import(/* webpackChunkName: "about" */ '../views/about.vue')
    },
    {
        path: '/admin/user',
        name: 'AdminUser',
        component: AdminUser,
        meta: {

            key: '/admin/user'
        }
    },
    {
        path: '/admin/ebook',
        name: 'AdminEbook',
        component: AdminEbook,
        meta: {
            key: '/admin/ebook',
        }
    },
    {
        path: '/admin/category',
        name: 'AdminCategory',
        component: AdminCategory,
        meta: {
            key: '/admin/category',
        }
    },
    {
        path: '/admin/doc',
        name: 'AdminDoc',
        component: AdminDoc,
        meta: {
            key: '/admin/doc',
        }
    },
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})


// 在路由导航之前，从本地存储中恢复 meta 属性的值
router.beforeEach((to, from, next) => {
    const meta = to.meta;
    console.log(meta);

    localStorage.setItem('meta', JSON.stringify(meta));
    next();
});

// 在路由导航之后，将 meta 属性的值保存到本地存储


export default router
